"""
Case Type   : cm_ctl
Case Name   : 执行cm_ctl pause后,故障-kill -19 cma进程
Create At   : 2023/04/10
Owner       : opentestcase012
Description :
    1.执行cm_ctl pause;
    2.查看进程;
    3.故障-kill -19 cma进程;
    4.再次查看进程;
    5.恢复集群状态;
    6.查看集群状态;
Expect      :
    1.执行成功
    2.成功,cm_agent进程存在
    3.执行成功
    4.重新拉起cma进程(pid变化)
    5.恢复成功
    6.cm进行仲裁选主,集群状态恢复正常
History     :
"""
import os
import time
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node

primary_sh = CommonSH('PrimaryDbUser')


# @unittest.skipIf(2 != primary_sh.get_node_num(), '非1+2环境不执行')
class DefinedResource(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'----{os.path.basename(__file__)} start----')
        self.common = Common()
        self.constant = Constant()
        self.pri_user = Node('PrimaryDbUser')
        self.primary_sh = CommonSH('PrimaryDbUser')

    def test_cm(self):
        text = '----step1:执行cm_ctl pause; expect:暂停成功----'
        self.log.info(text)
        cm_ctl_res = self.primary_sh.exec_cm_ctl('pause')
        self.assertIn(self.constant.cm_pause_success_msg, cm_ctl_res,
                      '执行失败:' + text)

        text = '----step2:查看进程; expect:成功,cm_agent进程存在----'
        self.log.info(text)
        process_cmd = '''ps ux|grep "cm_agent"|grep -v grep|\
        tr -s ' '|cut -d ' ' -f 2'''
        self.log.info(process_cmd)
        process_old = self.common.get_sh_result(self.pri_user, process_cmd)
        self.assertNotEqual('', process_old, "执行失败:" + text)

        text = '----step3:故障-kill -19 cma进程; expect:执行成功----'
        self.log.info(text)
        kill_cmd = '''ps ux|grep "cm_agent"|grep -v grep|\
            tr -s ' '|cut -d ' ' -f 2|xargs kill -19;'''
        self.log.info(kill_cmd)
        process_dn = self.common.get_sh_result(self.pri_user, kill_cmd)
        self.assertEqual('', process_dn, "执行失败:" + text)

        time.sleep(5)
        text = '----step4:再次查看进程; expect:重新拉起cma进程(pid变化)----'
        self.log.info(text)
        self.log.info(process_cmd)
        process_new = self.common.get_sh_result(self.pri_user, process_cmd)
        self.assertNotEqual('', process_new, "执行失败:" + text)
        self.assertNotEqual(process_new, process_old, "执行失败:" + text)

        text = '----step5:恢复集群状态; expect:恢复成功----'
        self.log.info(text)
        cm_ctl_res = self.primary_sh.exec_cm_ctl('resume')
        self.assertIn(self.constant.cm_resume_success_msg, cm_ctl_res,
                      '执行失败:' + text)

        time.sleep(30)
        text = '----step6:查看集群状态; expect:cm进行仲裁选主,集群状态恢复正常----'
        self.log.info(text)
        cm_ctl_res = self.primary_sh.exec_cm_ctl('query', '-Civ')
        self.assertIn(self.constant.CLUSTER_NORMAL_MSG, cm_ctl_res,
                      '执行失败:' + text)
        self.assertNotIn('pausing         : Yes', cm_ctl_res, '执行失败:' + text)

    def tearDown(self):
        self.log.info('-----清理环境-----')
        text = '----恢复集群状态; expect:执行成功----'
        self.log.info(text)
        cm_ctl_res1 = self.primary_sh.exec_cm_ctl('resume')
        cm_ctl_res2 = self.primary_sh.exec_cm_ctl('start')
        cm_ctl_res3 = self.primary_sh.exec_cm_ctl('switchover', '-a')
        time.sleep(10)
        cm_ctl_res4 = self.primary_sh.exec_cm_ctl('query', '-Civ')
        self.assertIn(self.constant.cm_resume_success_msg, cm_ctl_res1,
                      '执行失败:' + text)
        self.assertIn(self.constant.cm_start_success_msg, cm_ctl_res2,
                      '执行失败:' + text)
        self.assertIn(self.constant.cm_switchover_success_msg, cm_ctl_res3,
                      '执行失败:' + text)
        self.assertIn(self.constant.CLUSTER_NORMAL_MSG, cm_ctl_res4,
                      "执行失败:" + text)
        self.log.info(f'----{os.path.basename(__file__)} finish----')
